//
//  addCarImagePickerTableViewCell.m
//  CarTown
//
//  Created by xionghao on 15/9/12.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "addCarImagePickerTableViewCell.h"
#import "SDPhotoBrowser.h"
#import "LCActionSheet.h"
@interface addCarImagePickerTableViewCell ()<LCActionSheetDelegate>
@property (weak, nonatomic) IBOutlet UIView *photoview;

@end

@implementation addCarImagePickerTableViewCell
+(instancetype)cellWithTablView:(UITableView *)tablview{
    addCarImagePickerTableViewCell *cell=[tablview dequeueReusableCellWithIdentifier:@"addphoto"];
    if (cell==nil) {
        [tablview registerNib:[UINib nibWithNibName:NSStringFromClass([self class]) bundle:nil] forCellReuseIdentifier:@"addphoto"];
        cell=[tablview dequeueReusableCellWithIdentifier:@"addphoto"];

    }
    return cell;
}
- (void)awakeFromNib {
    // Initialization code
    CGFloat x=0;
    CGFloat y=0;
    CGFloat width=60;
    CGFloat leftx=0;
    CGFloat height=width;
    CGFloat boder=(XHScreenwidth-66-4*width)/3;
    if (boder>8) {
        boder=8;
       leftx =XHScreenwidth-66-4*width-24;
    }
    
    for (int i=0; i<8; i++) {
        UIButton *btn=[[UIButton alloc]init];
        [btn setImage:[UIImage imageNamed:@"button_photo"] forState:UIControlStateNormal];
        int col=i%4;
        int row=i/4;
        x=leftx+(boder+width)*col;
        y=(8+height)*row;
        btn.frame=CGRectMake(x, y, width, height);
        btn.tag=i;
        btn.hidden=!(i==7);
        btn.imageView.contentMode=UIViewContentModeScaleAspectFill;
        
        [btn addTarget:self action:@selector(pickerbtnclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.photoview addSubview:btn];
        
    }

}


-(void)setMode:(CarpubMode *)mode{
    _mode=mode;
    [self reloadImage];
}


-(void)reloadImage{
    
        for (int i=0; i<8; i++) {
            UIButton *btn=self.photoview.subviews[i];
            if (i<_mode.imgarray.count) {
                [btn setImage:_mode.imgarray[i] forState:UIControlStateSelected];
                btn.selected=YES;
                btn.hidden=NO;
            }else{
                btn.selected=NO;
                if (_mode.imgarray.count<4&&i==3) {
                    btn.hidden=NO;
                }else if(_mode.imgarray.count>=4&&i==7){
                    btn.hidden=NO;
                }else{
                    btn.hidden=YES;
                }
            }
        }
    
}

-(void)pickerbtnclick:(UIButton *)btn{
    
    if (btn.isSelected) {
        [self showhasactionsheet:btn.tag];
    }else{
        [self showactionsheet:btn.tag];
    }
}
-(void)showactionsheet:(NSInteger)tag{
    
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles: @[@"拍照",@"从相册中选取"] redButtonIndex:-1 delegate:self];
    sheet.isTag=NO;
    sheet.tag=tag;
    [sheet show];
}
-(void)showhasactionsheet:(NSInteger)tag{
    LCActionSheet *sheet=[[LCActionSheet alloc]initWithTitle:nil buttonTitles: @[@"重新上传",@"查看大图",@"删除"] redButtonIndex:2 delegate:self];
    sheet.isTag=YES;
    sheet.tag=tag;
    [sheet show];
}

-(void)removeImageFromArray:(NSInteger)index{
    [self.mode.imgarray removeObjectAtIndex:index];
    [self reloadImage];
    if ([_delegate respondsToSelector:@selector(photoCellViewReloadImage:)]) {
        [_delegate photoCellViewReloadImage:self];
    }
    
}

-(void)showImageWith:(NSInteger)index{
    SDPhotoBrowser *browser=[[SDPhotoBrowser alloc]init];
    browser.sourceImagesContainerView = self.photoview; // 原图的父控件
    browser.imageCount = _mode.imgarray.count; // 图片总数
    browser.currentImageIndex = index;
    browser.delegate = self;
    [browser show];
}

-(UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index{
    return self.mode.imgarray[index];
}

-(void)actionSheet:(LCActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==actionSheet.btnCount-1) {
        return;
    }
    if (actionSheet.isTag) {
        switch (buttonIndex) {
            case 0:
                [self showactionsheet:actionSheet.tag];
                break;
            case 1:
                [self showImageWith:actionSheet.tag];
                break;
            case 2:
            {
                [self removeImageFromArray:actionSheet.tag];
            }
                break;
            default:
                break;
        }
        
        
        
    }else{
        [self showImagepicker:actionSheet.tag ActionSheetIndex:buttonIndex];
    }
}

-(void)showImagepicker:(NSInteger)index ActionSheetIndex:(NSInteger) actionindex{
    if ([_delegate respondsToSelector:@selector(photoCellView:pickerBtnClick:withButton:)]) {
        [_delegate photoCellView:self pickerBtnClick:actionindex withButton: _photoview.subviews[index]];
    }
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
